package cn.yunwei.module.atl.vo;
import cn.yunwei.module.atl.token.TokenFetcher;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author wangbinggui
 */
@Component
public class JsonDataHandler {


    private static final String TARGET_URL = "http://example.com/api/endpoint";

    /**
     * 发送 JSON 数据到目标 URL
     */
    public void sendJsonData() {
        // 获取 Token 直接通过类名调用静态方法
        String token = TokenFetcher.fetchToken();

        if (token == null) {
            System.err.println("无法获取 Token！");
            return;
        }

        // 创建 JSON 数据
        Map<String, Object> jsonData = createJsonData(token);

        // 创建 RestTemplate 实例
        RestTemplate restTemplate = new RestTemplate();

        // 设置请求头
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.set("Authorization", "Bearer " + token);

        // 创建请求实体
        HttpEntity<Map<String, Object>> requestEntity = new HttpEntity<>(jsonData, headers);

        try {
            // 发送 POST 请求
            ResponseEntity<String> response = restTemplate.postForEntity(TARGET_URL, requestEntity, String.class);
            System.out.println("Response Status Code: " + response.getStatusCode());
            System.out.println("Response Body: " + response.getBody());
        } catch (Exception e) {
            System.err.println("Error sending JSON data: " + e.getMessage());
        }
    }

    /**
     * 创建 JSON 数据
     *
     * @param token 访问令牌
     * @return JSON 数据
     */
    private Map<String, Object> createJsonData(String token) {
        Map<String, Object> header = new HashMap<>();
        header.put("applicationCode", "ATL");
        header.put("applicationGroupCode", "ATL");
        header.put("batchCount", 1);
        header.put("batchNum", "XXXX");
        header.put("externalSystemCode", "XXXXX");
        header.put("interfaceCode", "SUPPLIER_TX_KEYDATA");
        header.put("userName", "interface-admin");
        header.put("access_token", token);
        header.put("token_type", "");

        List<Map<String, Object>> items = new ArrayList<>();

        for (int i = 1; i <= 20; i++) {
            Map<String, Object> item = new HashMap<>();
            item.put("returnHeaderUuid", "1234202503120000" + i);
            item.put("asnNo", "500XXXXX" + i);
            // 添加其他字段...
            items.add(item);
        }

        Map<String, Object> body = new HashMap<>();
        body.put("items", items);

        Map<String, Object> jsonData = new HashMap<>();
        jsonData.put("header", header);
        jsonData.put("body", body);

        return jsonData;
    }
}